/*
 * Created on Nov 3, 2003
 * 
 * To change the template for this generated file go to Window - Preferences -
 * Java - Code Generation - Code and Comments
 */
package org.vectrics.recruiter.struts;

import java.util.Locale;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.vectrics.DataException;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.user.Person;
import org.vectrics.user.PersonEducationType;
import org.vectrics.user.web.struts.SaveUserAction;
import org.vectrics.user.web.struts.UserProfileForm;

public class SavePublicProfileAction extends SaveUserAction {
    private transient static Logger log = Logger.getLogger(SavePublicProfileAction.class);

    public static String USER_PARAM = "userSearch";

    public static String FULL_TIME_PARAM = "fullTime";

    public static String PART_TIME_PARAM = "partTime";

    public static String CONTRACT_PARAM = "contractWork";

    public static String PERMANENT_PARAM = "permWork";

    public static String TRAVEL_PARAM = "travel";

    protected boolean getAuthUserMustBeSavingOwnInfo() {
        return (true);
    }

    protected boolean getEmailRequired() {
        return (true);
    }

    protected boolean getGroupsRequired() {
        return (false);
    }

    protected boolean getEmailMustBeUnique() {
        return (RecruitConfig.getRecruitInstance().getEmailMustBeUnique(ThreadProperties.getDomain()));
    }

    protected boolean getRolesRequired() {
        return (false);
    }

    protected boolean getUserNameRequired() {
        return (true);
    }

    protected boolean getSaveHomeAddress() {
        return (true);
    }

    protected boolean getSavePersonName() {
        return (true);
    }

    protected boolean getSaveEmails() {
        return (true);
    }

    protected boolean getSavePhoneNumbers() {
        return (true);
    }

    protected boolean getSaveWorkAddress() {
        return (true);
    }

    protected boolean getSaveRoles() {
        return (false);
    }

    protected boolean getSavePassword(UserProfileForm form) {
        return (false);
    }

    protected void onUserAdded(Person addedUser, Person userWhoDidIt, Locale locale) {
    }

    protected void onUserChanged(Person changedUser, Person userWhoDidIt, Locale locale) {
    }

    protected void saveExtendedData(Person user, VectricsActionData actionData, ActionErrors errors)
            throws JspException {
        log.info("SavePublicProfileAction.doSave() called");
        RecruitUserProfileForm form = (RecruitUserProfileForm) actionData.getForm();

        String userParam = form.getUserName();

        if (user == null) {
            log.error("SavePublicProfileAction.doPerform() - could not find user: " + userParam);
            throw new JspException("doPerform() - could not find user: " + userParam);
        } else {
            try {
                log.debug("SavePublicProfileAction.doSave() - setting candidate data");
                CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService()
                        .createCandidateInfoIfNotExist(user);
                candidateInfo.setVoided(true);
                candidateInfo.setLookingFullTime(form.getLookingFullTime());
                candidateInfo.setLookingPartTime(form.getLookingPartTime());
                candidateInfo.setLookingPermanent(form.getLookingPermanent());
                candidateInfo.setLookingContract(form.getLookingContract());
                candidateInfo.setOkToContact(form.getOkToContact());
                candidateInfo.setCurrentlyWorking(form.getCurrentlyWorking());
                candidateInfo.setTravelPercent(new Integer(form.getTravelPercent()));
                candidateInfo.setCareerGoals(form.getCareerGoals());

                PersonEducationType personEducationType = CoreServiceLocator.getPersonService()
                        .findPersonEducationType(form.getHighestEducationCode());
                candidateInfo.setHighestEducation(personEducationType);
                log.info("Person highest education set to: " + personEducationType);
                RecruitServiceLocator.getRecruitUserService().updateCandidateInfo(candidateInfo);
            } catch (DataException de) {
                log.error("DataException in doSave()", de);
            }
        }
        log.debug("SavePublicProfileAction.doPerform() - finished.");
    }

}
